/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import { DrawerForm, ProFormDigit, ProFormSelect } from '@ant-design/pro-form';
import { message, Form, Button } from 'antd';
import type { ConnectState } from '@/models/connect';
import type { ProFormInstance } from '@ant-design/pro-form';
import { useSelector } from 'umi';
import KbAddressSelect from '@/components/KbAddressSelect';
import { setAreaBasePrice } from '@/services/jdk/areaPrice';
import { deliveryTypeMap } from '../_utils';
import { getBrandList } from '@/services/jdk/shield';
import { isFunction } from 'lodash';

type PropsType = {
  record?: any;
  onRefresh?: any;
};

const AddAreaPriceModal: React.FC<PropsType> = (props) => {
  const { record = {}, onRefresh } = props;
  const { id = '' } = record || {};
  const formRef = useRef<ProFormInstance>();
  const { winSize } = useSelector((state: ConnectState) => state.global);

  const handleFinish = async (values: any) => {
    console.log('values', JSON.stringify(values.area));
    const reqData = {
      ...values,
      base_price: (values.base_price * 100).toFixed(2),
      id,
    };
    const { code, msg } = await setAreaBasePrice(reqData);
    if (code == 0) {
      message.success('操作成功！');
      if (isFunction(onRefresh)) {
        onRefresh();
      }
      return true;
    } else {
      message.error(msg);
    }
    return false;
  };

  // useEffect(() => {
  //   return () => {
  //     formRef.current?.resetFields();
  //   };
  // }, []);

  return (
    <DrawerForm
      title={`${id ? '修改' : '设置'}区域基础价`}
      formRef={formRef}
      width={winSize === 'xs' ? '80%' : 500}
      labelAlign='left'
      layout='horizontal'
      labelCol={{
        span: 5,
      }}
      drawerProps={{
        destroyOnClose: true,
      }}
      initialValues={{
        brand: record?.brand,
        limit_type: record?.limit_type || record?.limit_type === 0 ? record?.limit_type + '' : null,
        area: record?.area_code_list,
        base_price: record?.base_price ? (record?.base_price / 100).toFixed(2) : null,
      }}
      onFinish={handleFinish}
      trigger={
        props.children ? (
          <div>{props.children}</div>
        ) : (
          <Button type='primary'>添加区域基础价</Button>
        )
      }
    >
      <Form.Item name='area' label='请选择区域' rules={[{ required: true, message: '请选择区域' }]}>
        <KbAddressSelect level={2} multiple placeholder='请选择区域' />
      </Form.Item>
      <ProFormSelect
        name='limit_type'
        label='运力类型'
        placeholder='请选择运力类型'
        fieldProps={{
          allowClear: false,
        }}
        valueEnum={deliveryTypeMap}
        rules={[{ required: true, message: '请选择运力类型' }]}
      />
      <ProFormSelect
        name='brand'
        label='选择运力'
        placeholder='请选择运力品牌'
        request={getBrandList}
        rules={[{ required: true, message: '请选择运力品牌' }]}
      />
      <ProFormDigit
        name='base_price'
        label='基础价格'
        placeholder='请输入基础价格'
        rules={[{ required: true, message: '请输入基础价格' }]}
        min={0}
        max={3}
        fieldProps={{
          precision: 2,
        }}
        addonAfter='元'
      />
    </DrawerForm>
  );
};

export default AddAreaPriceModal;
